if(!dojo._hasResource["opm.widgets.component.ClassInst"]){
dojo._hasResource["opm.widgets.component.ClassInst"] = true;
dojo.provide("opm.widgets.component.ClassInst");
dojo.require("dijit._Widget");
dojo.require("opm.widgets.Ajax");
dojo.require("opm.widgets.GenericForm");
dojo.require("opm.widgets.GenericPage");
dojo.require("dijit.Dialog");
dojo.declare("opm.widgets.component.ClassInst",dijit._Widget,
{
   contentId:"",
   defaultURL:"",
   _GENERIC_PAGE:"opm.widgets.component.ClassInst.genericPage",
   _GENERIC_CREATE_FORM:"opm.widgets.component.ClassInst.createForm",
   _GENERIC_INFO_FORM:"opm.widgets.component.ClassInst.infoForm",
   _GENERIC_EDIT_FORM:"opm.widgets.component.ClassInst.editForm",
   
   constructor:function(args){
      this.setAtts(args);
   },
   setAtts:function(args){
      this.contentId = args.contentId;
   },

   showAddForm:function(ajaxUrl){
      var args = {ajaxUrl: ajaxUrl};
      OPM.ajaxWidget(args, this.contentId).callXhrGet();
   },

   showEditForm:function(ajaxUrl){
      var localDetailForm = this._GENERIC_INFO_FORM;
      var dataPage = {
         ajaxCallUrl: ajaxUrl,
         ajaxCallType:'callXhrGetJson',
         contructCurrentForm: function(data, contentId){
            var dataform = {
               dataform: data,
               contentId:contentId,
               widgetId:localDetailForm 
            };
            var genericForm = OPM.genericFormWidget(dataform);
            return genericForm;
         },
         contentId: this.contentId,
         widgetId: this._GENERIC_PAGE
      };
      var _w=this;
      var genericPage = OPM.genericPageWidget(dataPage);
      genericPage.createPage();
      genericPage.handleEvent("checkinClick", function(){
         OPM.classInstWidget().showModifyForm(genericPage.getAtt("checkinUrl"));
      });
      genericPage.handleEvent("checkoutClick", function(){
         var genericFormDialog = null;
         var checkoutUrl = genericPage.getAtt("checkoutUrl");
         var handleClickBtnYesFunction = {
            checkoutUrl: checkoutUrl,
            callBackFunction: function(){
                genericFormDialog.callPostText({
                  ajaxUrl: this.checkoutUrl,
                  handleSuccess: function(data){
                     genericFormDialog.close();
                     OPM.classInstWidget().showEditForm(genericPage.getAtt("informationUrl"));
                  }
               });
            }
         };
         var dataForm = {title:genericPage.getAtt("checkoutDialogTitle"), content:genericPage.getAtt("checkoutDialogContent"), handleClickBtnYes:handleClickBtnYesFunction};
         genericFormDialog = OPM.genericFormDialogWiget(dataForm, 'center');
         genericFormDialog.showDialog();
      });
      genericPage.handleEvent("cancelCheckoutClick", function(){
         var genericFormDialog = null;
         var checkoutUrl = genericPage.getAtt("cancelCheckoutUrl");
         var handleClickBtnYesFunction = {
            checkoutUrl: checkoutUrl,
            callBackFunction: function(){
                genericFormDialog.callPostText({
                  ajaxUrl: this.checkoutUrl,
                  handleSuccess: function(data){
                     genericFormDialog.close();
                     OPM.classInstWidget().showEditForm(genericPage.getAtt("informationUrl"));
                  }
               });
            }
         };
         var dataForm = {title:genericPage.getAtt("cancelCheckoutDialogTitle"), content:genericPage.getAtt("cancelCheckoutDialogContent"), handleClickBtnYes:handleClickBtnYesFunction};
         genericFormDialog = OPM.genericFormDialogWiget(dataForm, 'center');
         genericFormDialog.showDialog();
      });
      genericPage.handleEvent("downloadClick", function(){
         var genericFormDialog = null;
         var downloadUrl = genericPage.getAtt("downloadUrl");
         var handleClickBtnYesFunction = {
            downloadUrl: downloadUrl,
            callBackFunction: function(){
                genericFormDialog.close();
                dojo.io.iframe.send({url:this.downloadUrl, timeout:4000});
            }
         };
         var dataForm = {title:genericPage.getAtt("downloadDialogTitle"), content:genericPage.getAtt("downloadDialogContent"), handleClickBtnYes:handleClickBtnYesFunction};
         genericFormDialog = OPM.genericFormDialogWiget(dataForm, 'center');
         genericFormDialog.showDialog();
      });
      genericPage.handleEvent("modifyClick", function(){
         _w.showModifyForm(genericPage.getAtt("modifyUrl"));
      });
      genericPage.handleEvent("historyClick", function(){
         var args = {ajaxUrl: genericPage.getAtt("historyUrl")};
         OPM.ajaxWidget(args, 'center').callXhrGet();
      });
      genericPage.handleEvent("securityClick", function(){
         _w._showSecurities(genericPage.getAtt("securityUrl"));
      });
      genericPage.handleEvent("syncsecurityClick", function(){
         _w._showSyncSecurities(genericPage.getAtt("synSecurityUrl"));
      });
      genericPage.handleEvent("deleteClick", function(){
         _w._delete(genericPage.getAtt("deleteUrl"),genericPage);
      });
   },

   showModifyForm:function(ajaxUrl){
       var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: this.contentId,
         widgetId: this._GENERIC_EDIT_FORM
      };
      var genericForm = this.commonForm(dataform);
      genericForm.createForm();
   },

   showCreateForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: this.contentId,
         widgetId: this._GENERIC_CREATE_FORM
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
         OPM.classDefWidget().displayClassDefMenu();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({handleSuccess:function(data){
             var secondDlg = new dijit.Dialog({
                    title: data.title,
                    style: "width: 300px",
                    content: data.content
                });
              secondDlg.show();
              OPM.classDefWidget().displayClassDefMenu();
         }});
      });
      genericForm.createForm();
   },

   _showSecurities:function(ajaxUrl){
      var args = {ajaxUrl:ajaxUrl,callback:this._initInstSecurity};
      OPM.ajaxWidget(args,'center').callXhrGet();
   },
   _showSyncSecurities:function(ajaxUrl){
      var args = {ajaxUrl:ajaxUrl,callback:this._initInstSyncSecurity};
      OPM.ajaxWidget(args,'center').callXhrGet();
   },
   _delete:function(deleteUrl,currentPage){
      var genericFormDialog = null;
      var handleClickBtnYesFunction = {
            deleteUrl: deleteUrl,
            callBackFunction: function(){
                genericFormDialog.callPostText({
                  ajaxUrl: this.deleteUrl,
                  handleSuccess: function(data){
                     genericFormDialog.close();
                     OPM.classDefWidget().displayClassDefMenu();
                  }
               });
            }
      };
      var dataForm = {title:currentPage.getAtt("deleteDialogTitle"),content:currentPage.getAtt("deleteDialogContent"),handleClickBtnYes:handleClickBtnYesFunction};
      genericFormDialog = OPM.genericFormDialogWiget(dataForm, 'center');
      genericFormDialog.showDialog();
   },
   
   commonForm:function(dataform){
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
          OPM.classDefWidget().displayClassDefMenu();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({
            handleSuccess:function(){
                OPM.classDefWidget().displayClassDefMenu();
            }
         });
      });
      return genericForm;
   },

   _initInstSecurity:function(){
      var instEditUrl = dojo.byId('hrefInstEdit').value;
      var listUrl = dojo.byId('hrefListSecurities').value;
      dojo.query('table#sec tbody tr').connect('ondblclick', function(evt){
         var editUrl = dojo.query('input[type="hidden"]', this)[0].value;
         OPM.securityWidget({},'center').showEditForm(editUrl,{cancel:function(){OPM.classInstWidget()._showSecurities(listUrl);},
            succ:function(){OPM.classInstWidget()._showSecurities(listUrl);}});
      });
      dojo.query('#hrefCancel').onclick(function(){
         OPM.classInstWidget({},'center').showEditForm(instEditUrl);
      });
      dojo.query('#hrefAssignPermision').onclick(function(){
         var searchUrl = dojo.query('input[type="hidden"]', this)[0].value;
         OPM.securityWidget({},'center').showSecuritiesForm(searchUrl,function(){
            OPM.classInstWidget()._showSecurities(listUrl);
         });
      });
      dojo.query('#hrefRestoreCreator').onclick(function(){
         var restoreCreatorUrl = dojo.query('input[type="hidden"]', this)[0].value;
         OPM.ajaxWidget({
            ajaxUrl: restoreCreatorUrl,
            callback: function (data){
               OPM.classInstWidget()._showSecurities(listUrl);
            }
         },'center').callXhrPostText();
      });
   },
   _initInstSyncSecurity:function(){
      var instEditUrl = dojo.byId('hrefInstEdit').value;
      dojo.query('#hrefCancel').onclick(function(){
         OPM.classInstWidget({},'center').showEditForm(instEditUrl);
      });
      dojo.query('#hrefSync').onclick(function(){
         var syncUrl = dojo.query('input[type="hidden"]',this)[0].value;
         OPM.ajaxWidget({
            ajaxUrl: syncUrl,
            callback: function (data){
               OPM.classInstWidget({},'center').showEditForm(instEditUrl);
            }
         },'center').callXhrPostText();
      });
   }
}
);
}
